import type { PluginDTO, SignatureStatus, SignatureType } from '@/types/plugin'

// 模拟插件数据
const mockPlugins: PluginDTO[] = [
  {
    id: '1',
    name: '数据源管理',
    type: 'datasource',
    enabled: true,
    tags: ['数据源', '管理'],
    pinned: true,
    info: {
      author: {
        name: 'CaiPu Team',
        url: 'https://caipu.com',
      },
      description: '管理各种数据源的插件',
      links: [
        {
          name: '文档',
          url: 'https://docs.caipu.com/datasource',
        },
      ],
      logos: {
        small: 'https://doc.gocpu.cn/logo.png',
        large: 'https://doc.gocpu.cn/logo.png',
      },
      build: {
        time: 1632145890000,
        repo: 'github.com/caipu/datasource',
        branch: 'main',
        hash: 'a1b2c3d4',
      },
      screenshots: [
        {
          name: '数据源管理界面',
          path: '/assets/screenshots/datasource-1.png',
        },
      ],
      version: '1.2.0',
      updated: '2023-09-15',
    },
    dependencies: {
      caiPuDependency: '>=9.0.0',
      caiPuVersion: '9.1.0',
      plugins: [],
    },
    latestVersion: '1.2.0',
    hasUpdate: false,
    defaultNavUrl: '/plugins/datasource',
    category: 'data',
    signature: 'valid' as SignatureStatus,
    signatureType: 'caipu' as SignatureType,
    signatureOrg: 'CaiPu',
    readme: '# 数据源管理插件\n\n这是一个用于管理数据源的插件。',
    versions: [
      {
        version: '1.2.0',
        createdAt: '2023-09-15',
        isLatest: true,
      },
      {
        version: '1.1.0',
        createdAt: '2023-06-10',
        isLatest: false,
      },
    ],
  },
  {
    id: 'workflow-app',
    name: '可视化面板',
    type: 'panel',
    enabled: true,
    tags: ['可视化', '图表'],
    pinned: false,
    info: {
      author: {
        name: 'CaiPu Team',
        url: 'https://caipu.com',
      },
      description: '提供各种可视化图表的插件',
      links: [
        {
          name: '文档',
          url: 'https://docs.caipu.com/panel',
        },
      ],
      logos: {
        small: '/assets/panel-small.png',
        large: '/assets/panel-large.png',
      },
      build: {
        time: 1635145890000,
        repo: 'github.com/caipu/panel',
        branch: 'main',
        hash: 'e5f6g7h8',
      },
      screenshots: [
        {
          name: '可视化面板示例',
          path: '/assets/screenshots/panel-1.png',
        },
      ],
      version: '2.0.1',
      updated: '2023-10-20',
    },
    latestVersion: '2.0.1',
    hasUpdate: false,
    defaultNavUrl: '/plugins/panel',
    category: 'visualization',
    signature: 'valid' as SignatureStatus,
    signatureType: 'caipu' as SignatureType,
    signatureOrg: 'CaiPu',
  },
  {
    id: '3',
    name: '告警管理',
    type: 'alert',
    enabled: false,
    tags: ['告警', '监控'],
    pinned: false,
    info: {
      author: {
        name: 'Community',
        url: 'https://community.caipu.com',
      },
      description: '管理和配置告警规则的插件',
      links: [
        {
          name: '文档',
          url: 'https://docs.caipu.com/alert',
        },
      ],
      logos: {
        small: '/assets/alert-small.png',
        large: '/assets/alert-large.png',
      },
      build: {
        time: 1638145890000,
        repo: 'github.com/caipu-community/alert',
        branch: 'main',
        hash: 'i9j0k1l2',
      },
      screenshots: [
        {
          name: '告警配置界面',
          path: '/assets/screenshots/alert-1.png',
        },
      ],
      version: '1.5.0',
      updated: '2023-11-05',
    },
    latestVersion: '1.6.0',
    hasUpdate: true,
    defaultNavUrl: '/plugins/alert',
    category: 'monitoring',
    signature: 'valid' as SignatureStatus,
    signatureType: 'community' as SignatureType,
    signatureOrg: 'Community',
  },
  {
    id: '4',
    name: '日志分析',
    type: 'logs',
    enabled: false,
    tags: ['日志', '分析'],
    pinned: false,
    info: {
      author: {
        name: 'Third Party',
        url: 'https://thirdparty.com',
      },
      description: '高级日志分析和查询工具',
      links: [
        {
          name: '文档',
          url: 'https://thirdparty.com/logs-docs',
        },
      ],
      logos: {
        small: '/assets/logs-small.png',
        large: '/assets/logs-large.png',
      },
      build: {
        time: 1641145890000,
        repo: 'github.com/thirdparty/logs',
        branch: 'main',
        hash: 'm3n4o5p6',
      },
      screenshots: [
        {
          name: '日志分析界面',
          path: '/assets/screenshots/logs-1.png',
        },
      ],
      version: '3.1.0',
      updated: '2023-12-15',
    },
    latestVersion: '3.1.0',
    hasUpdate: false,
    defaultNavUrl: '/plugins/logs',
    category: 'analysis',
    signature: 'unsigned' as SignatureStatus,
    signatureType: 'private' as SignatureType,
    signatureOrg: 'Third Party',
  },
  {
    id: '1',
    name: '数据源管理',
    type: 'datasource',
    enabled: true,
    tags: ['数据源', '管理'],
    pinned: true,
    info: {
      author: {
        name: 'CaiPu Team',
        url: 'https://caipu.com',
      },
      description: '管理各种数据源的插件',
      links: [
        {
          name: '文档',
          url: 'https://docs.caipu.com/datasource',
        },
      ],
      logos: {
        small: '/assets/datasource-small.png',
        large: '/assets/datasource-large.png',
      },
      build: {
        time: 1632145890000,
        repo: 'github.com/caipu/datasource',
        branch: 'main',
        hash: 'a1b2c3d4',
      },
      screenshots: [
        {
          name: '数据源管理界面',
          path: '/assets/screenshots/datasource-1.png',
        },
      ],
      version: '1.2.0',
      updated: '2023-09-15',
    },
    dependencies: {
      caiPuDependency: '>=9.0.0',
      caiPuVersion: '9.1.0',
      plugins: [],
    },
    latestVersion: '1.2.0',
    hasUpdate: false,
    defaultNavUrl: '/plugins/datasource',
    category: 'data',
    signature: 'valid' as SignatureStatus,
    signatureType: 'caipu' as SignatureType,
    signatureOrg: 'CaiPu',
    readme: '# 数据源管理插件\n\n这是一个用于管理数据源的插件。',
    versions: [
      {
        version: '1.2.0',
        createdAt: '2023-09-15',
        isLatest: true,
      },
      {
        version: '1.1.0',
        createdAt: '2023-06-10',
        isLatest: false,
      },
    ],
  },
  {
    id: '2',
    name: '可视化面板',
    type: 'panel',
    enabled: true,
    tags: ['可视化', '图表'],
    pinned: false,
    info: {
      author: {
        name: 'CaiPu Team',
        url: 'https://caipu.com',
      },
      description: '提供各种可视化图表的插件',
      links: [
        {
          name: '文档',
          url: 'https://docs.caipu.com/panel',
        },
      ],
      logos: {
        small: '/assets/panel-small.png',
        large: '/assets/panel-large.png',
      },
      build: {
        time: 1635145890000,
        repo: 'github.com/caipu/panel',
        branch: 'main',
        hash: 'e5f6g7h8',
      },
      screenshots: [
        {
          name: '可视化面板示例',
          path: '/assets/screenshots/panel-1.png',
        },
      ],
      version: '2.0.1',
      updated: '2023-10-20',
    },
    latestVersion: '2.0.1',
    hasUpdate: false,
    defaultNavUrl: '/plugins/panel',
    category: 'visualization',
    signature: 'valid' as SignatureStatus,
    signatureType: 'caipu' as SignatureType,
    signatureOrg: 'CaiPu',
  },
  {
    id: '3',
    name: '告警管理',
    type: 'alert',
    enabled: false,
    tags: ['告警', '监控'],
    pinned: false,
    info: {
      author: {
        name: 'Community',
        url: 'https://community.caipu.com',
      },
      description: '管理和配置告警规则的插件',
      links: [
        {
          name: '文档',
          url: 'https://docs.caipu.com/alert',
        },
      ],
      logos: {
        small: '../assets/alert-small.png',
        large: '../assets/alert-large.png',
      },
      build: {
        time: 1638145890000,
        repo: 'github.com/caipu-community/alert',
        branch: 'main',
        hash: 'i9j0k1l2',
      },
      screenshots: [
        {
          name: '告警配置界面',
          path: '../assets/screenshots/alert-1.png',
        },
      ],
      version: '1.5.0',
      updated: '2023-11-05',
    },
    latestVersion: '1.6.0',
    hasUpdate: true,
    defaultNavUrl: '/plugins/alert',
    category: 'monitoring',
    signature: 'valid' as SignatureStatus,
    signatureType: 'community' as SignatureType,
    signatureOrg: 'Community',
  },

]

// 模拟API服务
export const pluginApi = {
  // 获取所有插件
  getAllPlugins: (): Promise<PluginDTO[]> => {
    return new Promise((resolve, reject) => {
      setTimeout(() => {
        // 模拟网络错误
        reject(new Error('Failed to fetch plugins from gocpu.cn'))
        // 正常情况下的代码（已注释掉）
        // resolve([...mockPlugins])
      }, 500)
    })
  },

  // 获取已安装的插件
  getInstalledPlugins: (): Promise<PluginDTO[]> => {
    return new Promise((resolve) => {
      setTimeout(() => {
        resolve(mockPlugins.filter((plugin) => plugin.enabled))
      }, 500)
    })
  },

  // 获取单个插件详情
  getPluginById: (id: string): Promise<PluginDTO> => {
    return new Promise((resolve, reject) => {
      setTimeout(() => {
        const plugin = mockPlugins.find((p) => p.id === id)
        if (plugin) {
          // 模拟添加更多详细信息
          const detailedPlugin = {
            ...plugin,
            readMe: `# ${plugin.name}\n\n${plugin.info.description}\n\n## 功能特点\n\n- 功能1：提供强大的数据处理能力\n- 功能2：简单易用的界面\n- 功能3：丰富的配置选项\n\n## 使用方法\n\n1. 安装插件\n2. 配置插件\n3. 开始使用`,
            changeLog: `# 更新日志\n\n## ${plugin.info.version}\n\n- 新增功能A\n- 修复问题B\n- 优化性能C\n\n## 1.0.0\n\n- 初始版本发布`,
            versions: [
              {
                version: plugin.info.version,
                date: plugin.info.updated,
                notes: `- 新增功能A\n- 修复问题B\n- 优化性能C`,
              },
              {
                version: '1.0.0',
                date: '2023-01-01',
                notes: `- 初始版本发布`,
              },
            ],
          }
          resolve(detailedPlugin)
        } else {
          reject(new Error('插件不存在'))
        }
      }, 500)
    })
  },
}
